import 'package:flutter/material.dart';

class FixedList extends StatelessWidget {
  List data;
  Function onRefresh;
  Function loadMore;
  ScrollController _scrollController = new ScrollController();
  FixedList({this.data, this.onRefresh, this.loadMore}){
    _scrollController
      ..addListener(() {
        var triggerFetchMoreSize =
            0.9 * _scrollController.position.maxScrollExtent;

        if (_scrollController.position.pixels > triggerFetchMoreSize) {
          // call fetch more method here
          print('more');
          loadMore();
        }
      });
  }


  @override
  Widget build(BuildContext context) {
    openDetail(item) async {
      final url = item.url;
      Navigator.pushNamed(context, url);
    }
    List<Widget> getListItem() {
      if (data == null) {
        return [];
      }
      return data.map((item) {
        if (item == null) {
          return Container();
        }

        return GestureDetector(
          onTap: () => openDetail(item),
          child: Container(
            padding: const EdgeInsets.symmetric(vertical: 18.0),
            decoration: BoxDecoration(
                border:Border(bottom:BorderSide(width: 1,color: Colors.grey[200]) )
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(
                  item.title,
                  style: TextStyle(fontSize: 16),
                ),
                SizedBox(
                  height: 20,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children: [
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('15.00%',style: TextStyle(color: Colors.blueAccent[400],fontWeight: FontWeight.bold,fontSize: 25),),
                        Text('年华收益率',style: TextStyle(color: Colors.grey),)
                      ],
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('14 天',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                        Text('期限',style: TextStyle(color: Colors.grey),)
                      ],
                    ),
                    Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text('100 USDT',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15),),
                        Text('起投金额',style: TextStyle(color: Colors.grey),)
                      ],
                    )
                  ],
                )
              ],
            ),
          ),
        );
      }).toList();
    }
    return Expanded(
      child: RefreshIndicator(
        onRefresh: () async {
          await onRefresh();
        },
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 18.0),
          child: ListView(
            controller: _scrollController,
            shrinkWrap: true,
            children: getListItem(),
          ),
        ),
      ),
    );
  }
}

